const app = getApp()
Page({
  data: {
    allSelected: false,
    list: [{
      status: true
    }, {
      status: true
    }],
    totprice: 0,
    leave_save:true
  },
  bindinfo: function () {
    wx.navigateTo({
      url: '../info/info'
    })

  },
  examineAllStatus: function () {
    var list = this.data.list;
    var allSelected = true;
    for (var item of list) {
      if (!item.status) {
        allSelected = false;
        break;
      }
    }
    this.setData({
      allSelected: allSelected
    })
  },
  allSelectedClick: function () {
    var allSelected = !this.data.allSelected;
    var list = this.data.list;

    for (var item of list) {
      item.status = allSelected;
    }
    this.setData({
      list: list,
      allSelected: allSelected
    })
    this.countPrice()
    
  },
  chooseprof: function (event) {
    console.log(event)
    var index = event.currentTarget.dataset.index;
    var list = this.data.list;
    var item = list[index];
    item.status = !item.status;
    this.setData({
      list: list
    })
    this.examineAllStatus();
    this.countPrice()
  },
  reduce: function (event) {

    var index = event.currentTarget.dataset.index;
    console.log(index)
    var list = this.data.list;
    list[index].pronum -= 1
    if (list[index].pronum == 0) {
      list.splice(index, 1);
    }
    this.setData({
      list: list
    })
    wx.setStorageSync('shop_pro', list)
    this.countPrice()
    app.gettotnum();
  },
  add: function (event) {
    var index = event.currentTarget.dataset.index;
    var list = this.data.list;
    list[index].pronum += 1
    this.setData({
      list: list
    })
    wx.setStorageSync('shop_pro', list)
    this.countPrice();
    app.gettotnum();
  },
  countPrice: function () {

    //  var list =this.data.list
    //   var totprice=0
    //   for (let item of list){
    //     if (item.status==true){
    //       totprice = totprice + item.pronum * parseInt(item.proobj.price)
    //     }
    //   }
    //   this.setData({
    //     totprice: totprice
    //   })
    this.buy()
  },
  buy: function () {
    var that = this
    var items = []
    var item = {}
    for (let item2 of this.data.list) {
      if (item2.status == true) {
        item.productId = item2.proobj.productId
        item.specs = item2.proobj.specs
        item.qty = item2.pronum
        items.push(item)
        item = {}
      }
    }
    console.log('items', JSON.stringify(items))
    // wx.navigateTo({
    //   url: '../order/order'
    // })
    //   app.req("post", '/api/carts/inquery/', { 
    //     // "token": wx.getStorageSync('token'),  
    //     "items": JSON.stringify(items)},
    //      function (res) {

    //   }.bind(this))
    wx.request({
      url: app.globalData.orderUrl+'/api/carts/inquery/',
      method: 'get',
      data: {
        session: wx.getStorageSync('session'),
        items: { 'items': items },
      },
      header: {
        // 'content-type': "application/x-www-form-urlencoded",
      },
      success: function (res) {
        if (res.data.status == 4201) {
          console.log(res.data.data[0].onerror)
          var minip = res.data.data[0].onerror.params
          wx.navigateToMiniProgram({
            appId: minip.appid,
            path: minip.path + '?session=' + wx.getStorageSync('session'),
            extraData: {
              foo: 'bar'
            },
            envVersion: minip.version,
            success(res) {
              // 打开成功
            }
          })
        }
        else {
          that.setData({
            totprice: res.data.cartTotal.total
          })
        }
      }
    })
  }
  ,
  preorder: function () {
    var that = this
    var items = []
    var item = {}
    var yu_p=[];
    for (let item2 of this.data.list) {
      if (item2.status == true) {
        item.productId = item2.proobj.productId
        item.specs = item2.proobj.specs
        item.qty = item2.pronum
        items.push(item)
        item = {}
      }else{
        yu_p.push(item2)
      }
    }
    if (items.length == 0) {
      wx.showToast({
        title: '请选择商品',
        icon: 'none',
        duration: 1000
      })
      return;
    }
    wx.request({
      url: app.globalData.orderUrl+'/api/s/orders/create/',
      method: 'GET',
      data: {
        "session": wx.getStorageSync('session'),
        'orderId': '',
        items: { 'items': items },
      },
      header: {
        'content-type': "application/x-www-form-urlencoded",
      },
      success: function (res) {
        if (res.data.status == 4201) {
          console.log(res.data.data[0].onerror)
          var minip = res.data.data[0].onerror.params
          wx.navigateToMiniProgram({
            appId: minip.appid,
            path: minip.path + '?session=' + wx.getStorageSync('session') + '&back=' + minip.data.back,
            extraData: {
              // back: minip.data.back,
              back: true,
              session: wx.getStorageSync('session')
            },
            envVersion: minip.version,
            success(res) {
              // 打开成功
            }
          })
        }
        if (res.data.status == 200) {
          console.log('200')
          wx.setStorageSync('order', res.data.data[0])
          wx.setStorageSync('shop_pro', yu_p)
          that.setData({
            leave_save:false
          })
          wx.navigateTo({
            url: '../order/order'
          })

        }
      }
    })
  },
  onLoad(options) {
    // console.log('gwc')
    // console.log(options)
    // if (options != undefined)
    // wx.setStorageSync('session', options.session)
  },
  onShow(options) {
    app.userLogin()
    app.gettotnum();
    console.log('gwc session' + wx.getStorageSync('session'))
    var repro = wx.getStorageSync('shop_pro')
    console.log(repro)
    if (wx.getStorageSync('shop_pro') == '') {
      repro = []
    }
    this.setData({
      list: repro
    })
    var totst = repro.every(function (itme) {
      return itme.status == true
    })
    if (repro.length==0)
      totst=false;
    this.setData({
      allSelected: totst
    })
    this.countPrice()
  },

  onHide() {
    if (this.data.leave_save){
  wx.setStorage({
    key: 'shop_pro',
    data: this.data.list
  })
}
  },
  onShareAppMessage: function () {
    return {
      title: '十里芳菲',
      path: 'pages/route2/index/index'
    }
  }
})